//
//  YZChatMessageVoiceCell.m
//  CocoaAsyncSocketClient
//
//  Created by lixiaoyi on 2018/6/25.
//  Copyright © 2018年 lixiaoyi. All rights reserved.
//

#import "YZChatMessageVoiceCell.h"
#import "YZMessageModel.h"
#import "YZRecordManager.h"
#import "ChatHeader.h"
#import "YZMessageConst.h"

@interface YZChatMessageVoiceCell()

@property (nonatomic, strong) UIButton *voiceButton;
@property (nonatomic, strong) UILabel *durationLabel;
@property (nonatomic, strong) UIImageView *voiceIcon;
@property (nonatomic, strong) UIView *redView;


@end

@implementation YZChatMessageVoiceCell


- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self.contentView addSubview:self.voiceIcon];
        [self.contentView addSubview:self.durationLabel];
        [self.contentView addSubview:self.voiceButton];
        [self.contentView addSubview:self.redView];
    }
    
    return self;
}

- (void)setModelFrame:(YZMessageFrame *)modelFrame {
    [super setModelFrame:modelFrame];
    
//    NSString *voicePath = [self mediaPath:modelFrame.model.mediaPath];
//    self.durationLabel.text = [NSString stringWithFormat:@"%ld''",[[YZRecordManager shareManager] durationWithVideo:[NSURL fileURLWithPath:voicePath]]];
    
    
    // /var/mobile/Containers/Data/Application/79951129-042E-4723-8429-8E1BB08FC2DF/Documents/2018-07-06/20180706185206543.aac
    NSString *voicePath = [[YZRecordManager shareManager] changeAudioPath:modelFrame.model.mediaPath];

    
    self.durationLabel.text = [NSString stringWithFormat:@"%ld''",[[YZRecordManager shareManager] getVideoDuration:[NSURL fileURLWithPath:voicePath]]];
    
    
    if (modelFrame.model.isSender) {
        self.voiceIcon.image = [UIImage imageNamed:@"right-3"];
        UIImage *image1 = [UIImage imageNamed:@"right-1"];
        UIImage *image2 = [UIImage imageNamed:@"right-2"];
        UIImage *image3 = [UIImage imageNamed:@"right-3"];
        self.voiceIcon.animationImages = @[image1,image2,image3];
    } else {
        self.voiceIcon.image = [UIImage imageNamed:@"left-3"];
        UIImage *image1 = [UIImage imageNamed:@"left-1"];
        UIImage *image2 = [UIImage imageNamed:@"left-2"];
        UIImage *image3 = [UIImage imageNamed:@"left-3"];
        self.voiceIcon.animationImages = @[image1,image2,image3];
    }
    
    self.voiceIcon.animationDuration = 0.8;
    if (modelFrame.model.message.status == ICMessageStatus_read) {
        self.redView.hidden = YES;
    } else if(modelFrame.model.message.status == ICMessageStatus_unRead){
        self.redView.hidden = NO;
    }
    
    self.durationLabel.frame = modelFrame.durationLabelF;
    self.voiceIcon.frame = modelFrame.voiceIconF;
    self.voiceButton.frame = modelFrame.bubbleViewF;
    self.redView.frame = modelFrame.redViewF;
}

- (NSString *)mediaPath:(NSString *)originPath {
    NSString *name = [[originPath lastPathComponent] stringByDeletingPathExtension];
    return [[YZRecordManager shareManager] receiveVoicePathWithFileKey:name];
}

#pragma mark - lazy load
- (UIButton *)voiceButton {
    if (! _voiceButton) {
        _voiceButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_voiceButton addTarget:self action:@selector(voiceButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
    }
    
    return _voiceButton;
}

#pragma mark - respond Method

- (void)voiceButtonClicked:(UIButton *)voiceBtn{
    voiceBtn.selected = !voiceBtn.selected;
    [self routerEventWithName:GXRouterEventVoiceTapEventName
                     userInfo:@{MessageKey : self.modelFrame,
                                VoiceIcon  : self.voiceIcon,
                                RedView    : self.redView
                                }];
}

- (UILabel *)durationLabel {
    if (!_durationLabel) {
        _durationLabel = [UILabel new];
        _durationLabel.font = MessageFont;
    }
    
    return _durationLabel;
}

- (UIImageView *)voiceIcon {
    if (!_voiceIcon) {
        _voiceIcon = [UIImageView new];
    }
    
    return _voiceIcon;
}

- (UIView *)redView {
    if (!_redView) {
        _redView = [UIView new];
        _redView.layer.masksToBounds = YES;
        _redView.layer.cornerRadius = 4.0f;
        _redView.backgroundColor = XZRGB(0xf05e4b);
    }
    
    return _redView;
}


- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
